package com.lk.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lk.springboot.service.LoginService;
import com.lk.springboot.vo.Result;
import com.lk.springboot.vo.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;

/**
 * @author lk
 * @date 2020/1/11 0011 21:12
 * @description: 用户个人数据相关模块
 */

@RestController
public class UsersController {

    @Autowired
    LoginService loginService;

    /**
     * 获取用户是否为地铁小游戏的新用户
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/isnew", method = RequestMethod.GET)
    public String getisnew(@CookieValue("token") String token) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            String result = loginService.getisnew(u_id);
            return result;
        }

    }


    /**
     * 调用接口，获取是否在地铁中
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/isinstation", method = RequestMethod.GET)
    public String getIsInStation(@CookieValue("token") String token) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
//            RestTemplate restTemplate = new RestTemplate();
//            restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));// 注入RestTemplate后,设置编码
//            String url = "http://59.110.174.204:7280/v1.0/api/app/trip/getBaseTrip";
//            HttpHeaders headers = new HttpHeaders();            // 设置请求头
//            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//            headers.add("Content-Type", "application/x-www-form-urlencoded");
//            headers.add("userId", "1");
//            MultiValueMap<String, String> map = new LinkedMultiValueMap<>();    // 请求信息（请求体）
//            map.add("user_id", "1");
//            map.add("service_id", "1");
//            map.add("trip_no", "1");
//            HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers); //将请求头 与 请求体 封装成完整的请求
//            ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);//对相应的 url 发送 请求
////        JSONObject jsonobject = JSONObject.parseObject(response.getBody());
////        JSONObject result = JSONObject.parseObject(jsonobject.getString("result"));
////        JSONObject trip = JSONObject.parseObject(result.getString("trip"));
//            // 解析出行程状态
//            String status = JSONObject.parseObject(JSONObject.parseObject(JSONObject.parseObject(response.getBody()).getString("result")).getString("trip")).getString("status");
//            Boolean isInStation = false;// 0 未进站, 1 行程中, 2 待支付, 3 已付款
//            if (status.equals("1"))
//                isInStation = true;
//            else
//                isInStation = false;
            Boolean isInStation = loginService.getIsInStation(u_id);
            Result result = new Result(ResultCode.SUCCESS);
            result.setData(isInStation);
            return JSON.toJSONString(result);
        }

    }

    /**
     * 修改是否在地铁中
     *
     * @param token
     * @param status
     * @return
     */
    @RequestMapping(value = "/setstatus", method = RequestMethod.POST)
    public String setGetIsInStation(@CookieValue("token") String token,
                                    @RequestParam(value = "status") boolean status) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            int sta = 0;//默认不在地铁中为false，0
            if (status == true)//在地铁中为true，1
                sta = 1;
            loginService.alterIsInStation(u_id, sta);
            Result result = new Result(ResultCode.SUCCESS);
            result.setData(status);
            return JSON.toJSONString(result);
        }

    }
}
